package com.pda.yuanjing.pages.sybom;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.pda.yuanjing.R;
import com.pda.yuanjing.beanout.CodeBomBeanOut;
import com.pda.yuanjing.message.MessageBus;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ItemAdapter extends BaseAdapter {
    private Context mContext;
    LayoutInflater inflater;
    List<CodeBomBeanOut.DataDTO.ListDTO> mlists = new ArrayList<>();
    private int ii = 0;
    private int selectedPosition = -1;

    public void setSelectedPosition(int position) {
        selectedPosition = position;
    }

    public ItemAdapter(Context context, List<CodeBomBeanOut.DataDTO.ListDTO> lists,int i) {
        this.mContext = context;
        inflater = LayoutInflater.from(context);
        mlists = lists;
        ii = i;
    }

    @Override
    public int getCount() {
        return mlists == null ? 0 : mlists.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_bom, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.tv1.setText("物料编码:" + mlists.get(position).getMaterialsCode());
        viewHolder.tv2.setText("物料名称:" + mlists.get(position).getMaterialsName());
        viewHolder.tv3.setText("物料规格:" + mlists.get(position).getMaterialsSpecs());
        viewHolder.tv4.setText("数量:" + mlists.get(position).getContainNums());
        if (mlists.get(position).getIsNeedScan().equals("1")) {
            viewHolder.tv5.setText("必扫:是");
        } else {
            viewHolder.tv5.setText("必扫:否");
        }
        viewHolder.tv6.setText("节点:" + mlists.get(position).getJieDianName());

        viewHolder.tvb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MessageBus messageBus = new MessageBus();
                messageBus.setIndex(position);
                messageBus.setValue(0);
                EventBus.getDefault().post(messageBus);
            }
        });

        if(ii==1){
            viewHolder.tvv.setVisibility(View.VISIBLE);
        }
        viewHolder.tvv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MessageBus messageBus = new MessageBus();
                messageBus.setIndex(position);
                messageBus.setValue(1);
                EventBus.getDefault().post(messageBus);
            }
        });

        if (selectedPosition == position) {
            viewHolder.ll.setBackgroundColor(Color.parseColor("#8a8a8a"));
        } else {
            viewHolder.ll.setBackgroundColor(Color.WHITE);
        }
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.tv1)
        TextView tv1;
        @BindView(R.id.tv2)
        TextView tv2;
        @BindView(R.id.tv3)
        TextView tv3;
        @BindView(R.id.tv4)
        TextView tv4;
        @BindView(R.id.tv5)
        TextView tv5;
        @BindView(R.id.tv6)
        TextView tv6;
        @BindView(R.id.tvb)
        TextView tvb;
        @BindView(R.id.tvv)
        TextView tvv;
        @BindView(R.id.ll)
        RelativeLayout ll;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}

